package com.elink.enginerring.video.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.common.Pagination;
import com.elink.enginerring.util.JSONUtil;
import com.elink.enginerring.video.co.CourseCo;
import com.elink.enginerring.video.dao.ChapterMapper;
import com.elink.enginerring.video.dao.CourseMapper;
import com.elink.enginerring.video.entity.Chapter;
import com.elink.enginerring.video.entity.Course;

@Service
public class CourseService implements ICourseService {
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private ChapterMapper chapterMapper;

	@Override
	public Map<String, Object> getList(CourseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", courseMapper.selectList(co));
		map.put("pagination", new Pagination(co, courseMapper.selectCount(co)));
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getChapter(int courseId) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,chapterMapper.getChapter(courseId));
	}

	@Override
	public Map<String, Object> addChapter(Chapter chapter) {
		chapterMapper.insertSelective(chapter);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> updateChapter(Chapter chapter) {
		chapterMapper.updateByPrimaryKeySelective(chapter);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> delChapter(int chapterId) {
		chapterMapper.deleteByPrimaryKey(chapterId);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> delCourse(String courseIds) {
		 courseMapper.delCourse(courseIds);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Object add(Course course) {
		course.setCreateTime(new Date());
		return courseMapper.insertSelective(course);
	}
}
